home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / ZMODEM / ZMR.C < prev   
Text File  |  1993-06-28  |  5KB  |  223 lines

  1. /*
  2.  * File: zmr.c 07-30-1989 Copyright 1988, 1989 Omen Technology Inc All Rights
  3.  * Reserved
  4.  *
  5.  *
  6.  *
  7.  * This module implements ZMODEM Run Length Encoding, an extension that was not
  8.  * funded by the original Telenet development contract.
  9.  *
  10.  * This software may be freely used for non commercial and educational (didactic
  11.  * only) purposes.    This software may also be freely used to support file
  12.  * transfer operations to or from licensed Omen Technology products.  Any
  13.  * programs which use part or all of this software must be provided in source
  14.  * form with this notice intact except by written permission from Omen
  15.  * Technology Incorporated.
  16.  *
  17.  * Use of this software for commercial or administrative purposes except when
  18.  * exclusively limited to interfacing Omen Technology products requires a per
  19.  * port license payment of $20.00 US per port (less in quantity).  Use of
  20.  * this code by inclusion, decompilation, reverse engineering or any other
  21.  * means constitutes agreement to these conditions and acceptance of
  22.  * liability to license the materials and payment of reasonable legal costs
  23.  * necessary to enforce this license agreement.
  24.  *
  25.  *
  26.  * Omen Technology Inc            FAX: 503-621-3745 Post Office Box 4681
  27.  * Portland OR 97208
  28.  *
  29.  * This code is made available in the hope it will be useful, BUT WITHOUT ANY
  30.  * WARRANTY OF ANY KIND OR LIABILITY FOR ANY DAMAGES OF ANY KIND.
  31.  *
  32.  * ZMODEM RLE compression and decompression functions
  33.  */
  34.  
  35. /* Send data subpacket RLE encoded with 32 bit FCS */
  36. void    zsdar32(char *buf, int length, int frameend)
  37. {
  38.     register int c, l, n;
  39.     register UNSL long crc;
  40.  
  41.     crc = 0xFFFFFFFFL;
  42.     l = *buf++ & 0377;
  43.     if (length == 1)
  44.     {
  45.         zsendline(l);
  46.         crc = UPDC32(l, crc);
  47.         if (l == ZRESC)
  48.         {
  49.             zsendline(1);
  50.             crc = UPDC32(1, crc);
  51.         }
  52.     } else
  53.     {
  54.         for (n = 0; --length >= 0; ++buf)
  55.         {
  56.             if ((c = *buf & 0377) == l && n < 126 && length > 0)
  57.             {
  58.                 ++n;
  59.                 continue;
  60.             }
  61.             switch (n)
  62.             {
  63.                 case 0:
  64.                     zsendline(l);
  65.                     crc = UPDC32(l, crc);
  66.                     if (l == ZRESC)
  67.                     {
  68.                         zsendline(0100);
  69.                         crc = UPDC32(0100, crc);
  70.                     }
  71.                     l = c;
  72.                     break;
  73.                 case 1:
  74.                     if (l != ZRESC)
  75.                     {
  76.                         zsendline(l);
  77.                         zsendline(l);
  78.                         crc = UPDC32(l, crc);
  79.                         crc = UPDC32(l, crc);
  80.                         n = 0;
  81.                         l = c;
  82.                         break;
  83.                     }
  84.                     /* **** FALL THRU TO **** */
  85.                 default:
  86.                     zsendline(ZRESC);
  87.                     crc = UPDC32(ZRESC, crc);
  88.                     if (l == 040 && n < 34)
  89.                     {
  90.                         n += 036;
  91.                         zsendline(n);
  92.                         crc = UPDC32(n, crc);
  93.                     } else
  94.                     {
  95.                         n += 0101;
  96.                         zsendline(n);
  97.                         crc = UPDC32(n, crc);
  98.                         zsendline(l);
  99.                         crc = UPDC32(l, crc);
  100.                     }
  101.                     n = 0;
  102.                     l = c;
  103.                     break;
  104.             }
  105.         }
  106.     }
  107.     xsendline(ZDLE);
  108.     xsendline(frameend);
  109.     crc = UPDC32(frameend, crc);
  110.  
  111.     crc = ~crc;
  112.     for (length = 4; --length >= 0;)
  113.     {
  114.         zsendline((int) crc);
  115.         crc >>= 8;
  116.     }
  117. }
  118.  
  119.  
  120. /* Receive data subpacket RLE encoded with 32 bit FCS */
  121. int        zrdatr32(register char *buf, int length)
  122. {
  123.     register int c;
  124.     register UNSL long crc;
  125.     register char *end;
  126.     register int d;
  127.  
  128.     crc = 0xFFFFFFFFL;
  129.     Rxcount = 0;
  130.     end = buf + length;
  131.     d = 0;                        /* Use for RLE decoder state */
  132.     while (buf <= end)
  133.     {
  134.         if ((c = zdlread()) & ~0377)
  135.         {
  136.           crcfoo:
  137.             switch (c)
  138.             {
  139.                 case GOTCRCE:
  140.                 case GOTCRCG:
  141.                 case GOTCRCQ:
  142.                 case GOTCRCW:
  143.                     d = c;
  144.                     c &= 0377;
  145.                     crc = UPDC32(c, crc);
  146.                     if ((c = zdlread()) & ~0377)
  147.                         goto crcfoo;
  148.                     crc = UPDC32(c, crc);
  149.                     if ((c = zdlread()) & ~0377)
  150.                         goto crcfoo;
  151.                     crc = UPDC32(c, crc);
  152.                     if ((c = zdlread()) & ~0377)
  153.                         goto crcfoo;
  154.                     crc = UPDC32(c, crc);
  155.                     if ((c = zdlread()) & ~0377)
  156.                         goto crcfoo;
  157.                     crc = UPDC32(c, crc);
  158.                     if (crc != 0xDEBB20E3)
  159.                     {
  160.                         zperr(badcrc);
  161.                         return ERROR;
  162.                     }
  163.                     Rxcount = length - (end - buf);
  164. #ifndef DSZ
  165.                     vfile("zrdatr32: %d %s", Rxcount,
  166.                           Zendnames[d - GOTCRCE & 3]);
  167. #endif
  168.                     return d;
  169.                 case GOTCAN:
  170.                     zperr("Sender Canceled");
  171.                     return ZCAN;
  172.                 case TIMEOUT:
  173.                     zperr("TIMEOUT");
  174.                     return c;
  175.                 default:
  176.                     zperr("Bad data subpacket");
  177.                     return c;
  178.             }
  179.         }
  180.         crc = UPDC32(c, crc);
  181.         switch (d)
  182.         {
  183.             case 0:
  184.                 if (c == ZRESC)
  185.                 {
  186.                     d = -1;
  187.                     continue;
  188.                 }
  189.                 *buf++ = c;
  190.                 continue;
  191.             case -1:
  192.                 if (c >= 040 && c < 0100)
  193.                 {
  194.                     d = c - 035;
  195.                     c = 040;
  196.                     goto spaces;
  197.                 }
  198.                 if (c == 0100)
  199.                 {
  200.                     d = 0;
  201.                     *buf++ = ZRESC;
  202.                     continue;
  203.                 }
  204.                 d = c;
  205.                 continue;
  206.             default:
  207.                 d -= 0100;
  208.                 if (d < 1)
  209.                     goto badpkt;
  210.               spaces:
  211.                 if ((buf + d) > end)
  212.                     goto badpkt;
  213.                 while (--d >= 0)
  214.                     *buf++ = c;
  215.                 d = 0;
  216.                 continue;
  217.         }
  218.     }
  219.   badpkt:
  220.     zperr("Data subpacket too long");
  221.     return ERROR;
  222. }
  223.